package com.micro.school.order.controller;

import com.micro.school.common.core.domain.R;
import com.micro.school.common.core.web.controller.BaseController;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.order.bo.PayStudentFeeBo;
import com.micro.school.order.domain.PayStudentFeePayment;
import com.micro.school.order.domain.PayStudentFeePaymentDetail;
import com.micro.school.order.entity.PayMethodStaticEntity;
import com.micro.school.order.entity.PayStatusEntity;
import com.micro.school.order.service.IPayStudentFeePaymentDetailService;
import com.micro.school.order.service.IPayStudentFeePaymentService;
import com.micro.school.order.service.IPayStudentFeeService;
import com.micro.school.order.vo.PayStudentFeeItemVo;
import com.micro.school.order.vo.PayStudentFeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询H5 学生账单
 */
@RestController
@RequestMapping("/pay/studentBill/h5")
public class PayStudentFeeH5Controller extends BaseController
{
    @Autowired
    private IPayStudentFeeService payStudentFeeService;

    @Autowired
    private IPayStudentFeePaymentDetailService detailService;

    @Autowired
    private IPayStudentFeePaymentService paymentService;

    /**
     * 账单管理
     */
    @GetMapping(value = "/student/{studentId}")
    public R<List<PayStudentFeeVo>> getStudentInfo(@PathVariable("studentId") Integer studentId)
    {
        List<PayStudentFeeVo> result = new ArrayList<>();
        PayStudentFeeBo payStudentFee = new PayStudentFeeBo();
        payStudentFee.setStudentId(studentId);
        List<PayStudentFeeVo> studentFeeVos = payStudentFeeService.selectPayStudentFeeList(payStudentFee);
        if(null!=studentFeeVos && !studentFeeVos.isEmpty()){
            result = studentFeeVos.stream()
                    .sorted(Comparator
                            .comparing(PayStudentFeeVo::getFeeYear, Comparator.nullsLast(Comparator.reverseOrder()))
                    )
                    .collect(Collectors.toList());

            for(PayStudentFeeVo feeVo:result){
                List<PayStudentFeeItemVo> feeItemVos = payStudentFeeService.queryDetailsByStudentFeeId(feeVo.getId().longValue());
                if(null!=feeItemVos){
                    for(PayStudentFeeItemVo itemVo:feeItemVos){
                        itemVo.setPayStatusName(PayStatusEntity.getPayMethodName(itemVo));
                    }
                }
                feeVo.setDetails(feeItemVos);
            }
        }
        return R.ok(result);
    }

    /**
     * 学费交费记录
     */
    @GetMapping(value = "/payment/{studentId}")
    public AjaxResult getStudentPaymentInfo(@PathVariable("studentId") Integer studentId)
    {
        List<PayStudentFeeVo> result = new ArrayList<>();
        PayStudentFeeBo payStudentFee = new PayStudentFeeBo();
        payStudentFee.setStudentId(studentId);
        logger.info("getStudentPaymentInfo studentId {}",studentId);
        List<PayStudentFeeVo> studentFeeVos = payStudentFeeService.selectPayStudentFeeList(payStudentFee);
        if(null!=studentFeeVos){
            logger.info("getStudentPaymentInfo studentFeeVos {}",studentFeeVos.size());
        }
        if(null!=studentFeeVos && !studentFeeVos.isEmpty()){
            result = studentFeeVos.stream()
                    .sorted(Comparator
                            .comparing(PayStudentFeeVo::getFeeYear, Comparator.nullsLast(Comparator.reverseOrder()))
                    )
                    .collect(Collectors.toList());

            for(PayStudentFeeVo feeVo:result){
                PayStudentFeePayment query = new PayStudentFeePayment();
                query.setStudentFeeId(feeVo.getId().longValue());
                query.setTenantId(feeVo.getTenantId().longValue());
                List<PayStudentFeePayment> feePayments = paymentService.selectPayStudentFeePaymentList(query);
                if(null!=feePayments){
                    logger.info("getStudentPaymentInfo feePayments {}",feePayments.size());
                }
                if(null==feeVo.getPayDetails()){
                    feeVo.setPayDetails(new ArrayList<>());
                }
                if(null!=feePayments && !feePayments.isEmpty()){
                    for(PayStudentFeePayment payment:feePayments){
                        List<PayStudentFeePaymentDetail> details = detailService.selectPayStudentFeePaymentDetailByPaymentId(payment.getId());
                        if(null==details){
                            continue;
                        }
                        if(null!=details){
                            logger.info("getStudentPaymentInfo details {}",details.size());
                        }
                        if(details.size()==0){
                            continue;
                        }
                        for(PayStudentFeePaymentDetail detail:details){
                            detail.setPayMethodName(PayMethodStaticEntity.getPayMethodName(detail));
                            detail.setPayStatusName(PayStatusEntity.getPayMethodName(detail));
                        }
                        feeVo.getPayDetails().addAll(details);
                    }
                }
            }
        }
        return success(result);
    }

}
